<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWidgetFormSelectDoubleList represents a multiple select displayed as a double list.
 *
 * This widget needs some JavaScript to work. So, you need to include the JavaScripts
 * files returned by the getJavaScripts() method.
 *
 * If you use symfony 1.2, it can be done automatically for you.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfWidgetFormSelectDoubleList.class.php 30760 2010-08-25 11:50:26Z fabien $
 */
class sfWidgetFormSelectDoubleListSop extends sfWidgetFormSelectDoubleList
{
  protected function configure($options = array(), $attributes = array())
  {
    $options['associated_first'] = isset($options['associated_first']) ? $options['associated_first'] : false;
    if(!isset($this->attributes['size'])) $this->attributes['size'] = 6;
    parent::configure($options, $attributes);
    $this->addOption('label_unassociated', 'Non associés');
    $this->addOption('label_associated', 'Associés');
    
    $associated_first = isset($options['associated_first']) ? $options['associated_first'] : true;

    if ($associated_first)
    {
      $associate_image = 'previous.png';
      $unassociate_image = 'next.png';
      $float = 'left';
      $unfloat = 'right';
    }
    else
    {
      $associate_image = 'next.png';
      $unassociate_image = 'previous.png';
      $float = 'right';
      $unfloat = 'left';
    }
    
    $this->addOption('template', <<<EOF
<div class="%class%">
  <div style="display:inline-block;" align="center">
    <div style="float: $float">
      <div class="double_list_label">%label_associated%</div>
        %associated%
    </div>
    <div style="float: $float; margin-top: 2em">
      %associate%
      <br />
      %unassociate%
    </div>
    <div style="float: $float">
      <div class="double_list_label">%label_unassociated%</div>
        <div style="width:100%; height: 28px;" align="$unfloat" class="list_filter">
          %filter%
        </div>
        %unassociated%
    </div>
  </div>
  <br style="clear: both" />
  <script type="text/javascript">
    sfDoubleList.init(document.getElementById('%id%'), '%class_select%');
    $('#unassociated_%id%').filterByText($('#filter_%id%'), true, document.getElementById('%id%'));
    $('#%id%').attr('size',Number($('#unassociated_%id%').attr('size')) + 2);
    $('#filter_%id%').css('width','98%');
  </script>
</div>
EOF
);
  }
  
  public function render($name, $value = null, $attributes = array(), $errors = array()) {
    $this->setOption('class', $this->getOption('class') . ' ' . $this->getAttribute('class'));
    $return = parent::render($name, $value, $attributes, $errors);
    
    return strtr($return, array(
      '%filter%'              => sprintf('<input type="text" id="filter_'.$this->generateId($name).'" name="filter_'.$name.'" placeholder="Rechercher un élément" autocomplete="off" /> ')
    ));
  }
  
  /**
   * Gets the JavaScript paths associated with the widget.
   *
   * @return array An array of JavaScript paths
   */
  public function getJavascripts()
  {
    return array('/sfFormExtraPlugin/js/double_list.js', '/sfFormExtraPlugin/js/jquery.autocompleter.js','jquery/plugins/selectbox/js/jquery.selectbox.js');
  }
}
